﻿using System.IO;

using NUnit.Framework;

using Microsoft.WowAddonStudio.FrameXml.Common;
using Microsoft.WowAddonStudio.FrameXml.Formats.Mpq;

using Microsoft.WowAddonStudio.FrameXml.Tests.Properties;

namespace Microsoft.WowAddonStudio.FrameXml.Tests.Common
{
    [TestFixture]
    public class ResourceFileLoaderTest
    {
        private const string mpqArchiveRelativePath = @"Data\enGB\locale-enGB.MPQ";
        private const string mpqMorpheusFontPath = @"Fonts\MORPHEUS.TTF";
        private const string physicalEulaPath = @"Data\enGB\eula.html";

        private string mpqArchivePath;
        private MpqArchive mpqArchive;

        private ResourceFileLoader resourceFileLoader;

        [SetUp]
        public void SetUp()
        {
            if (!Directory.Exists(Settings.Default.WowBasePath))
                Assert.Ignore("Cannot run test. Please set the World of Warcraft base path.");

            mpqArchivePath = Path.Combine(Settings.Default.WowBasePath, mpqArchiveRelativePath);

            // Create MPQ archive
            mpqArchive = new MpqArchive(mpqArchivePath);

            // Create resource file loader
            resourceFileLoader = new ResourceFileLoader();

            // Add WoW base path to resource file loader
            resourceFileLoader.BasePaths.Add(Settings.Default.WowBasePath);

            // Add MPQ archive to resource file loader
            resourceFileLoader.MpqArchives.Add(mpqArchive);
        }

        [TearDown]
        public void TearDown()
        {
            if (resourceFileLoader != null)
                resourceFileLoader.Dispose();
        }


        [Test]
        public void GetResourceFileStreamPhysicalFileTest()
        {
            using (Stream stream = resourceFileLoader.GetResourceFileStream(physicalEulaPath))
            {
                Assert.IsNotNull(stream, "GetResourceFileStream returned null for physical path.");
                Assert.AreNotEqual(0, stream.Length, "GetresourceFileStream returned empty stream.");
            }
        }

        [Test]
        public void GetResourceFileStreamMpqFileTest()
        {
            using (Stream stream = resourceFileLoader.GetResourceFileStream(mpqMorpheusFontPath))
            {
                Assert.IsNotNull(stream, "GetResourceFileStream returned null for MPQ path.");
                Assert.IsTrue(stream is MpqStream, "GetResourceFileStream should return MpqStream when path is resolved from an MPQ archive.");
                Assert.AreNotEqual(0, stream.Length, "GetresourceFileStream returned empty stream.");
            }
        }

        [Test]
        public void GetResourceFileStreamNonExistentFileTest()
        {
            using (Stream stream = resourceFileLoader.GetResourceFileStream(@"Data\enGB\JessicaAlba.blp"))
            {
                Assert.IsNull(stream, "GetResourceFileStream returned stream for non-existent path.");
            }
        }
    }
}
